﻿using System.Net;
using System.Net.Http.Json;

namespace Kz.Telegram.Bot.Infrastructure.Extensions
{
    /// <summary>
    /// HttpClient 拓展
    /// </summary>
    public static class HttpClientExtenisons
    {
        /// <summary>
        /// Post
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_client"></param>
        /// <param name="requestUrl"></param>
        /// <param name="obj"></param>
        /// <returns>(responseStr,HttpStatusCode)</returns>
        public static async Task<(string, HttpStatusCode)> SimplePostAsync<T>(this HttpClient _client, string requestUrl, T obj) where T : class
        {
            using JsonContent content = JsonContent.Create(obj);
            using HttpResponseMessage resp = await _client.PostAsync(requestUrl, content);
            if (!resp.IsSuccessStatusCode)
            {
                return (string.Empty, resp.StatusCode);
            }
            return (await resp.Content.ReadAsStringAsync(), resp.StatusCode);
        }

        /// <summary>
        /// Get
        /// </summary>
        /// <param name="_client"></param>
        /// <param name="requestUrl"></param>
        /// <returns>(responseStr,HttpStatusCode)</returns>
        public static async Task<(string, HttpStatusCode)> SimpleGetAsync(this HttpClient _client, string requestUrl)
        {
            using HttpResponseMessage resp = await _client.GetAsync(requestUrl);
            if (!resp.IsSuccessStatusCode)
            {
                return (string.Empty, resp.StatusCode);
            }
            return (await resp.Content.ReadAsStringAsync(), resp.StatusCode);
        }
    }
}